/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZEmailAddress;
/*     */ import com.zimbra.client.ZGetMessageParams;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZMessage.ZMimePart;
/*     */ import com.zimbra.client.ZPrefs;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.JSONElement;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.mailbox.MailSender;
/*     */ import com.zimbra.cs.mailbox.MailSender.PreSendMailListener;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.mail.ToXML.EmailType;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.InputStream;
/*     */ import java.io.StringReader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Calendar;
/*     */ import java.util.Collections;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSendAndReceive
/*     */   extends TestCase
/*     */ {
/*  63 */   private static final String NAME_PREFIX = TestSendAndReceive.class.getSimpleName();
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String REMOTE_USER_NAME = "user2";
/*  66 */   private static final Pattern PAT_RECEIVED = Pattern.compile("Received: .*from.*LHLO.*");
/*  67 */   private static final Pattern PAT_RETURN_PATH = Pattern.compile("Return-Path: (.*)");
/*     */   private String mOriginalSmtpSendAddAuthenticatedUser;
/*     */   private String mOriginalDomainSmtpPort;
/*     */   private String[] mOriginalSmtpHostname;
/*     */   public static final String PUBLIC_LIST_HEADER = "X-ZTest-PublicFile";
/*     */   private TestSendMailListener listener;
/*     */   
/*     */   public void setUp() throws Exception {
/*  75 */     cleanUp();
/*  76 */     Provisioning.getInstance().getConfig().addCustomMimeHeaderNameAllowed("X-Zimbra-Presend");
/*  77 */     Provisioning.getInstance().getConfig().addCustomMimeHeaderNameAllowed("X-ZTest-PublicFile");
/*  78 */     this.listener = new TestSendMailListener();
/*  79 */     this.mOriginalSmtpSendAddAuthenticatedUser = TestUtil.getConfigAttr("zimbraSmtpSendAddAuthenticatedUser");
/*  80 */     this.mOriginalDomainSmtpPort = TestUtil.getDomainAttr("user1", "zimbraSmtpPort");
/*  81 */     this.mOriginalSmtpHostname = Provisioning.getInstance().getLocalServer().getSmtpHostname();
/*     */   }
/*     */   
/*     */ 
/*     */   public void testAutoSendDraft()
/*     */     throws Exception
/*     */   {
/*  88 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/*  90 */     ZMailbox.ZOutgoingMessage outgoingMsg = new ZMailbox.ZOutgoingMessage();
/*  91 */     List<ZEmailAddress> addrs = new LinkedList();
/*  92 */     String senderAddr = TestUtil.getAddress("user1");
/*  93 */     addrs.add(new ZEmailAddress(senderAddr, null, null, "f"));
/*  94 */     String rcptAddr = TestUtil.getAddress("user2");
/*  95 */     addrs.add(new ZEmailAddress(rcptAddr, null, null, "t"));
/*  96 */     outgoingMsg.setAddresses(addrs);
/*  97 */     String subject = NAME_PREFIX + " autoSendDraft";
/*  98 */     outgoingMsg.setSubject(subject);
/*     */     
/*     */ 
/* 101 */     mbox.saveDraft(outgoingMsg, null, Integer.toString(6), System.currentTimeMillis() + 500L);
/*     */     
/*     */ 
/* 104 */     TestUtil.waitForMessage(mbox, "in:Sent " + subject);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 109 */     boolean deletedFromSent = false;
/* 110 */     int tries = 0;
/* 111 */     while ((!deletedFromSent) && (tries < 10)) {
/* 112 */       deletedFromSent = TestUtil.search(mbox, "in:Drafts " + subject).isEmpty();
/* 113 */       if (!deletedFromSent) {
/* 114 */         Thread.sleep(500L);
/*     */       }
/* 116 */       tries++;
/*     */     }
/* 118 */     assertTrue("message is still in the Drafts folder", deletedFromSent);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testReceivedHeaders()
/*     */     throws Exception
/*     */   {
/* 128 */     String sender = TestUtil.getAddress("user2");
/* 129 */     String recipient = TestUtil.getAddress("user1");
/* 130 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testReceivedHeaders()", recipient, sender);
/*     */     
/*     */ 
/* 133 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 134 */     List<ZMessage> messages = TestUtil.search(mbox, NAME_PREFIX);
/* 135 */     assertEquals("Unexpected message count", 1, messages.size());
/*     */     
/*     */ 
/* 138 */     ZGetMessageParams params = new ZGetMessageParams();
/* 139 */     params.setId(((ZMessage)messages.get(0)).getId());
/* 140 */     params.setRawContent(true);
/* 141 */     ZMessage message = mbox.getMessage(params);
/* 142 */     String content = message.getContent();
/*     */     
/*     */ 
/* 145 */     boolean foundReceived = false;
/* 146 */     boolean foundReturnPath = false;
/* 147 */     BufferedReader reader = new BufferedReader(new StringReader(content));
/* 148 */     String line = reader.readLine();
/* 149 */     while (line != null) {
/* 150 */       Matcher matcher = PAT_RECEIVED.matcher(line);
/* 151 */       if (matcher.matches()) {
/* 152 */         ZimbraLog.test.debug("Found " + line);
/* 153 */         foundReceived = true;
/*     */       }
/*     */       
/* 156 */       matcher = PAT_RETURN_PATH.matcher(line);
/* 157 */       if (matcher.matches()) {
/* 158 */         foundReturnPath = true;
/* 159 */         assertEquals("Sender doesn't match", sender, matcher.group(1));
/* 160 */         ZimbraLog.test.debug("Found " + line);
/*     */       }
/* 162 */       line = reader.readLine();
/*     */     }
/* 164 */     reader.close();
/*     */     
/* 166 */     assertTrue("Received header not found.  Content=\n" + content, foundReceived);
/* 167 */     assertTrue("Return-Path header not found.  Content=\n" + content, foundReturnPath);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testZimbraReceivedHeader()
/*     */     throws Exception
/*     */   {
/* 176 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/*     */ 
/* 179 */     String msgContent = new String(ByteUtil.getContent(new File(LC.zimbra_home.value() + "/unittest/testZimbraReceivedHeader.msg")));
/*     */     
/* 181 */     TestUtil.addMessageLmtp(new String[] { "user1" }, "user1", msgContent);
/*     */     
/*     */ 
/* 184 */     List<ZMessage> messages = TestUtil.search(mbox, "subject:testZimbraReceivedHeader");
/* 185 */     assertEquals("Unexpected message count", 1, messages.size());
/* 186 */     ZMessage msg = (ZMessage)messages.get(0);
/* 187 */     Calendar cal = Calendar.getInstance(mbox.getPrefs().getTimeZone());
/* 188 */     cal.setTimeInMillis(msg.getReceivedDate());
/* 189 */     assertEquals(2005, cal.get(1));
/* 190 */     assertEquals(1, cal.get(2));
/* 191 */     assertEquals(27, cal.get(5));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testAuthenticatedUserHeader()
/*     */     throws Exception
/*     */   {
/* 200 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/*     */ 
/* 203 */     TestUtil.setConfigAttr("zimbraSmtpSendAddAuthenticatedUser", "FALSE");
/* 204 */     String subject = NAME_PREFIX + " testAuthenticatedUserHeader false";
/* 205 */     TestUtil.sendMessage(mbox, "user1", subject);
/* 206 */     ZMessage msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/* 207 */     assertNull(TestUtil.getHeaderValue(mbox, msg, "X-Authenticated-User"));
/*     */     
/*     */ 
/* 210 */     TestUtil.setConfigAttr("zimbraSmtpSendAddAuthenticatedUser", "TRUE");
/* 211 */     subject = NAME_PREFIX + " testAuthenticatedUserHeader true";
/* 212 */     TestUtil.sendMessage(mbox, "user1", subject);
/* 213 */     msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/* 214 */     assertEquals(mbox.getName(), TestUtil.getHeaderValue(mbox, msg, "X-Authenticated-User"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testDomainSmtpSettings()
/*     */     throws Exception
/*     */   {
/* 222 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 223 */     String subject = NAME_PREFIX + " testDomainSmtpSettings 1";
/* 224 */     TestUtil.sendMessage(mbox, "user1", subject);
/* 225 */     TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */ 
/* 228 */     TestUtil.setDomainAttr("user1", "zimbraSmtpPort", "35");
/* 229 */     subject = NAME_PREFIX + " testDomainSmtpSettings 2";
/* 230 */     boolean sendFailed = false;
/*     */     try {
/* 232 */       TestUtil.sendMessage(mbox, "user1", subject);
/*     */     } catch (SoapFaultException e) {
/* 234 */       assertEquals("mail.TRY_AGAIN", e.getCode());
/* 235 */       sendFailed = true;
/*     */     }
/* 237 */     assertTrue("Message send should have failed", sendFailed);
/*     */   }
/*     */   
/*     */ 
/*     */   public void testBogusSmtpHostname()
/*     */     throws Exception
/*     */   {
/* 244 */     List<String> smtpHosts = new ArrayList();
/* 245 */     Collections.addAll(smtpHosts, this.mOriginalSmtpHostname);
/* 246 */     for (int i = 1; i <= 10; i++) {
/* 247 */       smtpHosts.add("bogushost" + i);
/*     */     }
/* 249 */     String[] hostsArray = new String[smtpHosts.size()];
/* 250 */     smtpHosts.toArray(hostsArray);
/* 251 */     Provisioning.getInstance().getLocalServer().setSmtpHostname(hostsArray);
/*     */     
/*     */ 
/* 254 */     String subject = NAME_PREFIX + " testBogusSmtpHostname";
/* 255 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 256 */     TestUtil.sendMessage(mbox, "user1", subject);
/* 257 */     TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMalformedContentType()
/*     */     throws Exception
/*     */   {
/* 266 */     String subject = NAME_PREFIX + " testMalformedContentType";
/* 267 */     MessageBuilder builder = new MessageBuilder().withFrom("user1").withToRecipient("user1").withSubject(subject).withAttachment("This is an attachment", "test.txt", "text/plain");
/*     */     
/*     */ 
/*     */ 
/* 271 */     BufferedReader reader = new BufferedReader(new StringReader(builder.create()));
/* 272 */     StringBuilder msgBuf = new StringBuilder();
/* 273 */     String line = reader.readLine();
/* 274 */     boolean replaced = false;
/* 275 */     while (line != null) {
/* 276 */       if (line.matches("Content-Type.*test.txt.*")) {
/* 277 */         line = line.replace("Content-Type: text/plain;", "Content-Type: text/plain;;");
/* 278 */         assertTrue("Unexpected line: " + line, line.contains(";;"));
/* 279 */         replaced = true;
/*     */       }
/* 281 */       msgBuf.append(line).append("\r\n");
/* 282 */       line = reader.readLine();
/*     */     }
/* 284 */     assertTrue("Could not find text/plain attachment.", replaced);
/*     */     
/*     */ 
/* 287 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 288 */     TestUtil.addMessageLmtp(new String[] { "user1" }, "user1", msgBuf.toString());
/*     */     
/*     */ 
/* 291 */     ZMessage srcMsg = TestUtil.getMessage(mbox, "subject:\"" + subject + "\"");
/* 292 */     ZMessage.ZMimePart srcAttachPart = (ZMessage.ZMimePart)srcMsg.getMimeStructure().getChildren().get(1);
/* 293 */     assertEquals("test.txt", srcAttachPart.getFileName());
/*     */     
/* 295 */     ZMailbox.ZOutgoingMessage outgoing = new ZMailbox.ZOutgoingMessage();
/* 296 */     outgoing.setMessagePart(new ZMailbox.ZOutgoingMessage.MessagePart("text/plain", "Forwarding attachment."));
/* 297 */     outgoing.setMessagePartsToAttach(Arrays.asList(new ZMailbox.ZOutgoingMessage.AttachedMessagePart[] { new ZMailbox.ZOutgoingMessage.AttachedMessagePart(srcMsg.getId(), srcAttachPart.getPartName(), null) }));
/* 298 */     String address = TestUtil.getAddress("user1");
/* 299 */     ZEmailAddress sender = new ZEmailAddress(address, null, null, "f");
/* 300 */     ZEmailAddress recipient = new ZEmailAddress(address, null, null, "t");
/* 301 */     outgoing.setAddresses(Arrays.asList(new ZEmailAddress[] { sender, recipient }));
/* 302 */     String fwdSubject = NAME_PREFIX + " testMalformedContentType forward";
/* 303 */     outgoing.setSubject(fwdSubject);
/* 304 */     mbox.sendMessage(outgoing, null, false);
/*     */     
/*     */ 
/* 307 */     ZMessage fwd = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + fwdSubject + "\"");
/* 308 */     ZMessage.ZMimePart fwdAttachPart = (ZMessage.ZMimePart)fwd.getMimeStructure().getChildren().get(1);
/* 309 */     assertEquals("test.txt", fwdAttachPart.getFileName());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testTextAttachmentLineEnding()
/*     */     throws Exception
/*     */   {
/* 318 */     String content = "I used to think that the day would never come,\nI'd see the light in the shade of the morning sun\n";
/*     */     
/* 320 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 321 */     verifyTextAttachmentLineEnding(mbox, content, "text/plain");
/* 322 */     verifyTextAttachmentLineEnding(mbox, content, "application/x-shellscript");
/*     */   }
/*     */   
/*     */   public void testSendDraftWithData() throws Exception {
/* 326 */     MailSender.registerPreSendMailListener(this.listener);
/* 327 */     ZMailbox zmbox = TestUtil.getZMailbox("user1");
/* 328 */     String subj = "Thorin";
/* 329 */     String body = "far over the misty mountains cold";
/* 330 */     String customHeaderValue = "https://zss.server/file2";
/* 331 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/* 332 */     transport.setAuthToken(zmbox.getAuthToken());
/*     */     
/*     */ 
/* 335 */     Element request = new Element.JSONElement(MailConstants.SAVE_DRAFT_REQUEST);
/* 336 */     Element el = request.addUniqueElement("m");
/* 337 */     el.addAttribute("su", subj);
/* 338 */     el.addUniqueElement("mp").addAttribute("ct", "text/plain").addAttribute("content", body);
/*     */     
/*     */ 
/* 341 */     el.addNonUniqueElement("e").addAttribute("t", ToXML.EmailType.TO.toString()).addAttribute("a", TestUtil.addDomainIfNecessary("user2"));
/*     */     
/*     */ 
/* 344 */     el.addNonUniqueElement("header").addAttribute("name", "X-Zimbra-Presend").setText("custom");
/*     */     
/*     */ 
/* 347 */     el.addNonUniqueElement("header").addAttribute("name", "X-ZTest-PublicFile").setText(customHeaderValue);
/*     */     
/*     */ 
/*     */ 
/* 351 */     ZMessage draft = new ZMessage(transport.invoke(request).getElement("m"), zmbox);
/*     */     
/*     */ 
/* 354 */     request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
/* 355 */     request.addUniqueElement("m").addAttribute("did", draft.getId()).addAttribute("sfd", true).addAttribute("noSave", false);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 360 */     transport.invoke(request);
/*     */     
/* 362 */     assertTrue("Listener was not triggered", this.listener.isTriggered());
/* 363 */     assertEquals("listener should ahve been triggered only once", 1, this.listener.getTriggerCounter());
/* 364 */     String[] listenersData = this.listener.getData();
/* 365 */     assertNotNull("Listener did not get data from custom headers", listenersData);
/* 366 */     assertTrue("wrong number of elements in custom header data", listenersData.length == 1);
/* 367 */     assertTrue(String.format("wrong value in custom header: %s", new Object[] { listenersData[0] }), customHeaderValue.equalsIgnoreCase(listenersData[0].replace("\"", "")));
/*     */   }
/*     */   
/*     */   public void testSendDraftWithCustomDataNoSave() throws Exception {
/* 371 */     MailSender.registerPreSendMailListener(this.listener);
/* 372 */     MailSender.registerPreSendMailListener(this.listener);
/* 373 */     ZMailbox zmbox = TestUtil.getZMailbox("user1");
/* 374 */     String subj = "Dwalin";
/* 375 */     String body = "To dungeons deep, and caverns old";
/* 376 */     String customHeaderValue = "http://zss.server/file1";
/* 377 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/* 378 */     transport.setAuthToken(zmbox.getAuthToken());
/*     */     
/*     */ 
/* 381 */     Element request = new Element.JSONElement(MailConstants.SAVE_DRAFT_REQUEST);
/* 382 */     Element el = request.addUniqueElement("m");
/* 383 */     el.addAttribute("su", subj);
/* 384 */     el.addUniqueElement("mp").addAttribute("ct", "text/plain").addAttribute("content", body);
/*     */     
/*     */ 
/* 387 */     el.addNonUniqueElement("e").addAttribute("t", ToXML.EmailType.TO.toString()).addAttribute("a", TestUtil.addDomainIfNecessary("user2"));
/*     */     
/*     */ 
/* 390 */     el.addNonUniqueElement("header").addAttribute("name", "X-Zimbra-Presend").setText("custom");
/*     */     
/*     */ 
/* 393 */     el.addNonUniqueElement("header").addAttribute("name", "X-ZTest-PublicFile").setText(customHeaderValue);
/*     */     
/*     */ 
/*     */ 
/* 397 */     ZMessage draft = new ZMessage(transport.invoke(request).getElement("m"), zmbox);
/*     */     
/*     */ 
/* 400 */     request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
/* 401 */     request.addUniqueElement("m").addAttribute("did", Integer.parseInt(draft.getId())).addAttribute("sfd", true).addAttribute("noSave", true);
/*     */     
/*     */ 
/*     */ 
/* 405 */     transport.invoke(request);
/*     */     
/* 407 */     assertTrue("Listener was not triggered", this.listener.isTriggered());
/* 408 */     assertEquals("listener should ahve been triggered only once", 1, this.listener.getTriggerCounter());
/* 409 */     String[] listenersData = this.listener.getData();
/* 410 */     assertNotNull("Listener did not get data from custom headers", listenersData);
/* 411 */     assertTrue("wrong number of elements in custom header data", listenersData.length == 1);
/* 412 */     assertTrue(String.format("wrong value in custom header: %s", new Object[] { listenersData[0] }), customHeaderValue.equalsIgnoreCase(listenersData[0].replace("\"", "")));
/*     */   }
/*     */   
/*     */   public void testSendNoDraft() throws Exception {
/* 416 */     MailSender.registerPreSendMailListener(this.listener);
/* 417 */     MailSender.registerPreSendMailListener(this.listener);
/* 418 */     ZMailbox zmbox = TestUtil.getZMailbox("user1");
/* 419 */     String subj = "Dwalin";
/* 420 */     String body = "To dungeons deep, and caverns old";
/* 421 */     String customHeaderValue = "http://zss.server/file1";
/* 422 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/* 423 */     transport.setAuthToken(zmbox.getAuthToken());
/*     */     
/*     */ 
/* 426 */     Element request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
/* 427 */     Element el = request.addUniqueElement("m");
/* 428 */     el.addAttribute("su", subj);
/* 429 */     el.addUniqueElement("mp").addAttribute("ct", "text/plain").addAttribute("content", body);
/*     */     
/*     */ 
/* 432 */     el.addNonUniqueElement("e").addAttribute("t", ToXML.EmailType.TO.toString()).addAttribute("a", TestUtil.addDomainIfNecessary("user2"));
/*     */     
/*     */ 
/* 435 */     el.addNonUniqueElement("header").addAttribute("name", "X-Zimbra-Presend").setText("custom");
/*     */     
/*     */ 
/* 438 */     el.addNonUniqueElement("header").addAttribute("name", "X-ZTest-PublicFile").setText(customHeaderValue);
/*     */     
/*     */ 
/*     */ 
/* 442 */     transport.invoke(request);
/*     */     
/* 444 */     assertTrue("Listener was not triggered", this.listener.isTriggered());
/* 445 */     assertEquals("listener should ahve been triggered only once", 1, this.listener.getTriggerCounter());
/* 446 */     String[] listenersData = this.listener.getData();
/* 447 */     assertNotNull("Listener did not get data from custom headers", listenersData);
/* 448 */     assertTrue("wrong number of elements in custom header data", listenersData.length == 1);
/* 449 */     assertTrue("wrong value in custom header", customHeaderValue.equalsIgnoreCase(listenersData[0].replace("\"", "")));
/*     */   }
/*     */   
/*     */   public void testMultipleHeaders() throws Exception {
/* 453 */     MailSender.registerPreSendMailListener(this.listener);
/* 454 */     MailSender.registerPreSendMailListener(this.listener);
/* 455 */     ZMailbox zmbox = TestUtil.getZMailbox("user1");
/* 456 */     String subj = "Dwalin";
/* 457 */     String body = "To dungeons deep, and caverns old";
/* 458 */     String customHeaderValue1 = "http://zss.server/file1";
/* 459 */     String customHeaderValue2 = "http://zss.server/file2";
/* 460 */     String customHeaderValue3 = "http://zss.server/file3";
/*     */     
/* 462 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/* 463 */     transport.setAuthToken(zmbox.getAuthToken());
/*     */     
/*     */ 
/* 466 */     Element request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
/* 467 */     Element el = request.addUniqueElement("m");
/* 468 */     el.addAttribute("su", subj);
/* 469 */     el.addUniqueElement("mp").addAttribute("ct", "text/plain").addAttribute("content", body);
/*     */     
/*     */ 
/* 472 */     el.addNonUniqueElement("e").addAttribute("t", ToXML.EmailType.TO.toString()).addAttribute("a", TestUtil.addDomainIfNecessary("user2"));
/*     */     
/*     */ 
/* 475 */     el.addNonUniqueElement("header").addAttribute("name", "X-Zimbra-Presend").setText("custom");
/*     */     
/*     */ 
/* 478 */     el.addNonUniqueElement("header").addAttribute("name", "X-ZTest-PublicFile").setText(customHeaderValue1);
/*     */     
/*     */ 
/* 481 */     el.addNonUniqueElement("header").addAttribute("name", "X-ZTest-PublicFile").setText(customHeaderValue2);
/*     */     
/*     */ 
/* 484 */     el.addNonUniqueElement("header").addAttribute("name", "X-ZTest-PublicFile").setText(customHeaderValue3);
/*     */     
/*     */ 
/*     */ 
/* 488 */     transport.invoke(request);
/*     */     
/* 490 */     assertTrue("Listener was not triggered", this.listener.isTriggered());
/* 491 */     assertEquals("listener should ahve been triggered only once", 1, this.listener.getTriggerCounter());
/* 492 */     String[] listenersData = this.listener.getData();
/* 493 */     assertNotNull("Listener did not get data from custom headers", listenersData);
/* 494 */     assertEquals("wrong number of elements in custom header data", 3, listenersData.length);
/* 495 */     List<String> valueList = Arrays.asList(listenersData);
/* 496 */     assertTrue(customHeaderValue1 + " is missing", valueList.contains(String.format("\"%s\"", new Object[] { customHeaderValue1 })));
/* 497 */     assertTrue(customHeaderValue2 + " is missing", valueList.contains(String.format("\"%s\"", new Object[] { customHeaderValue2 })));
/* 498 */     assertTrue(customHeaderValue3 + " is missing", valueList.contains(String.format("\"%s\"", new Object[] { customHeaderValue3 })));
/*     */   }
/*     */   
/*     */   public void testSendNoHeaders() throws Exception {
/* 502 */     MailSender.registerPreSendMailListener(this.listener);
/* 503 */     ZMailbox zmbox = TestUtil.getZMailbox("user1");
/* 504 */     String subj = "Balin";
/* 505 */     String body = "We must away ere break of day";
/* 506 */     TestUtil.sendMessage(zmbox, "user2", subj, body);
/* 507 */     assertFalse("Listener was not supposed to be triggered", this.listener.isTriggered());
/*     */   }
/*     */   
/*     */   public void testSendDraftNoHeader() throws Exception {
/* 511 */     MailSender.registerPreSendMailListener(this.listener);
/* 512 */     ZMailbox zmbox = TestUtil.getZMailbox("user1");
/* 513 */     String subj = "Thorin";
/* 514 */     String body = "far over the misty mountains cold";
/* 515 */     ZMailbox.ZOutgoingMessage msg = TestUtil.getOutgoingMessage("user2", subj, body, null);
/*     */     
/* 517 */     ZMessage draft = zmbox.saveDraft(msg, null, Integer.toString(6));
/*     */     
/* 519 */     Element request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
/* 520 */     request.addUniqueElement("m").addAttribute("did", draft.getId()).addAttribute("sfd", true);
/* 521 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/* 522 */     transport.setAuthToken(zmbox.getAuthToken());
/* 523 */     transport.invoke(request);
/*     */     
/* 525 */     assertFalse("Listener was not supposed to be triggered", this.listener.isTriggered());
/*     */   }
/*     */   
/*     */   private void verifyTextAttachmentLineEnding(ZMailbox mbox, String content, String contentType)
/*     */     throws Exception
/*     */   {
/* 531 */     String attachId = mbox.uploadAttachment("text.txt", content.getBytes(), contentType, 5000);
/* 532 */     String subject = NAME_PREFIX + " testTextAttachmentLineEnding " + contentType + " 1";
/* 533 */     TestUtil.sendMessage(mbox, "user1", subject, "Testing text attachment", attachId);
/*     */     
/* 535 */     ZMessage msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/* 536 */     InputStream in = mbox.getRESTResource("?id=" + msg.getId() + "&part=2");
/* 537 */     String attachContent = new String(ByteUtil.getContent(in, content.length()));
/* 538 */     assertEquals(content, attachContent);
/*     */     
/*     */ 
/* 541 */     attachId = mbox.uploadAttachment("text.txt", content.getBytes(), contentType, 5000);
/* 542 */     subject = NAME_PREFIX + " testTextAttachmentLineEnding " + contentType + " 2";
/* 543 */     TestUtil.saveDraftAndSendMessage(mbox, "user1", subject, "Testing text attachment", attachId);
/*     */     
/* 545 */     msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/* 546 */     in = mbox.getRESTResource("?id=" + msg.getId() + "&part=2");
/* 547 */     attachContent = new String(ByteUtil.getContent(in, content.length()));
/* 548 */     assertEquals(content, attachContent);
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 554 */     cleanUp();
/* 555 */     TestUtil.setConfigAttr("zimbraSmtpSendAddAuthenticatedUser", this.mOriginalSmtpSendAddAuthenticatedUser);
/* 556 */     TestUtil.setDomainAttr("user1", "zimbraSmtpPort", this.mOriginalDomainSmtpPort);
/* 557 */     Provisioning.getInstance().getLocalServer().setSmtpHostname(this.mOriginalSmtpHostname);
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 562 */     Provisioning.getInstance().getConfig().removeCustomMimeHeaderNameAllowed("X-Zimbra-Presend");
/* 563 */     Provisioning.getInstance().getConfig().removeCustomMimeHeaderNameAllowed("X-ZTest-PublicFile");
/* 564 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 565 */     TestUtil.deleteTestData("user2", NAME_PREFIX);
/* 566 */     MailSender.unregisterPreSendMailListener(this.listener);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 571 */     TestUtil.cliSetup();
/* 572 */     TestUtil.runTest(TestSendAndReceive.class);
/*     */   }
/*     */   
/*     */   private class TestSendMailListener implements MailSender.PreSendMailListener {
/* 576 */     private boolean triggered = false;
/* 577 */     private String[] data = null;
/* 578 */     private int triggerCounter = 0;
/*     */     
/* 580 */     public TestSendMailListener() { this.triggerCounter += 1; }
/*     */     
/*     */ 
/*     */ 
/*     */     public void handle(Mailbox mbox, Address[] sentAddresses, MimeMessage sentMessage)
/*     */     {
/* 586 */       ZimbraLog.test.debug("Handling sent mail notification");
/* 587 */       this.triggered = true;
/*     */       try {
/* 589 */         this.data = sentMessage.getHeader("X-ZTest-PublicFile");
/* 590 */         if (this.data == null) {
/* 591 */           ZimbraLog.test.error("Could not find X-ZTest-PublicFile");
/*     */         }
/*     */       } catch (MessagingException e) {
/* 594 */         ZimbraLog.test.error("failed to extract custom mime header", e);
/*     */       }
/*     */     }
/*     */     
/*     */     public String[] getData() {
/* 599 */       return this.data;
/*     */     }
/*     */     
/*     */     public String getName()
/*     */     {
/* 604 */       return TestSendMailListener.class.getSimpleName();
/*     */     }
/*     */     
/*     */     public boolean isTriggered() {
/* 608 */       return this.triggered;
/*     */     }
/*     */     
/*     */     public int getTriggerCounter() {
/* 612 */       return this.triggerCounter;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSendAndReceive.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */